package com.yang.core.service;

import com.yang.core.dao.UserRepository;
import com.yang.core.model.User;
import com.yang.service.api.ShopService;
import com.yang.service.model.dto.get.ShopGetDTO;
import com.yang.service.model.dto.post.ShopPostDTO;
import com.yang.core.dao.ShopRepository;
import com.yang.core.model.Shop;
import com.yang.service.model.dto.put.ShopPutDTO;
import com.yang.service.model.vo.ShopVO;
import com.zhy.base.core.page.PageUtils;
import com.zhy.base.jpa.order.OrderHandler;
import com.zhy.base.service.page.PageResult;
import com.zhy.base.tools.beans.BeanMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;

/**
 * Created by xin7k
 * Date: 2019/12/2
 */
@Slf4j
@RestController
@RequestMapping("shop")
@Api(tags = "店铺操作API", description = "Shop")
public class ShopServiceImpl implements ShopService {
    @Autowired
    private BeanMapper beanMapper;
    @Autowired
    private ShopRepository shopRepository;
    @Autowired
    private UserRepository userRepository;


    @ApiOperation("保存店铺")
    @PostMapping
    @Transactional
    @Override
    public String save(@RequestBody @Valid ShopPostDTO dto) {
        Shop shop = beanMapper.map(dto, Shop.class);
        shop.setCreateTime(LocalDateTime.now());
        shop.setIsDeleted(0);
        shopRepository.save(shop);
        return null;
    }

    @ApiOperation("通过ID查询店铺")
    @Override
    @GetMapping("{id}")
    public ShopVO selectById(@PathVariable(value = "id") String id) {
        Shop shop = new Shop();
        shop.setId(id);
        Example<Shop> example = Example.of(shop);
        Optional<Shop> one = shopRepository.findOne(example);
        if(one.isPresent()){
            //通过用户ID 设置ShopVo的电话和拥有者
            User user = new User();
            user.setId(one.get().getOwnerId());
            Example<User> example1 = Example.of(user);
            Optional<User> one1 = userRepository.findOne(example1);
            ShopVO map = beanMapper.map(one.get(), ShopVO.class);
            map.setOwnerName(one1.get().getUserRealName());
            map.setPhone(one1.get().getPhone());
            return map;
        }else{
            return null;
        }
    }

    @Override
    public List<ShopVO> selectList(ShopGetDTO dto) {
        return null;
    }

    @Override
    public boolean update(String id, @Valid ShopPutDTO dto) {
        return false;
    }

    @Override
    public boolean deleteById(String id) {
        return false;
    }

    @Override
    public boolean saveBatch(@Valid List<ShopPostDTO> dtos) {
        return false;
    }

    @Override
    public boolean deleteBatch(List<String> strings) {
        return false;
    }


    @ApiOperation("查询指定数量店铺")
    @ApiImplicitParam(value = "数量", name = "number", required = true)
    @GetMapping("priority/{number}")
    @Override
    public PageResult<ShopVO> getPriorityShop(@PathVariable(name = "number") Integer number) {
        /*
         * 1.指定分页参数 (框架内部一起获取排序对象)
         * 2.根据example + pageable查询结果
         */
        Pageable pageable = OrderHandler.getPageable(0, number, Shop.class);

        Page<Shop> result = shopRepository.selectAll(Example.of(new Shop()), pageable);

        if (result.hasContent()) {
            List<ShopVO> vos = beanMapper.mapList(result.getContent(), ShopVO.class);
            return PageUtils.ofPage(result, vos);
        } else {
            return PageUtils.build(0, number, result.getTotalElements());
        }
    }

}
